#!/bin/bash
# httpclientandroidlib version
HTTPCLIENTANDROIDLIB_VER=1.1.2
CORE_VER=4.3
CLIENT_VER=4.3.1
CODEC_VER=1_6

# Checkout svn repositories of core/client/cache
URL_CORE_TAGS=https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags
URL_CLIENT_TAGS=https://svn.apache.org/repos/asf/httpcomponents/httpclient/tags
URL_CODEC_TAGS=https://svn.apache.org/viewvc/commons/proper/codec/tags/

svn checkout ${URL_CORE_TAGS}/${CORE_VER}/httpcore/ httpcore
svn checkout ${URL_CLIENT_TAGS}/${CLIENT_VER}/httpclient/ httpclient
svn checkout ${URL_CLIENT_TAGS}/${CLIENT_VER}/httpclient-cache/ httpclient-cache
svn checkout ${URL_CLIENT_TAGS}/${CLIENT_VER}/httpmime/ httpmime
svn checkout ${URL_CODEC_TAGS}/${CODEC_VER}/ codec


# Delete all .svn directories
find . -type d -name ".svn" -exec rm -Rf {} +

# Delete ehcache and memcached from httpclient-cache
rm -Rf httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ehcache
rm -Rf httpclient-cache/src/main/java/org/apache/http/impl/client/cache/memcached

PROJECTNAME=httpclientandroidlib
PACKAGENAME=ch.boye.httpclientandroidlib
ROOTDIR=`pwd`
SCRIPTDIR=$(dirname $(readlink -f "$0"))
SRCDIR=${ROOTDIR}/${PROJECTNAME}/src
PACKAGEDIR=${SRCDIR}/${PACKAGENAME//./\/}
ANDROIDPROJECTPATH=${ROOTDIR}/${PROJECTNAME}

# Create Android library project
rm -Rf ${ANDROIDPROJECTPATH}
android create lib-project -n "${PROJECTNAME}" -t "10" -p "${ANDROIDPROJECTPATH}" -k "${PACKAGENAME}"

# Create package directory
mkdir -p ${PACKAGEDIR}

# Copy all source files to new package directory
for MODULE in httpclient httpclient-cache httpmime httpcore
do
    cp -R ${ROOTDIR}/${MODULE}/src/main/java/org/apache/http/* ${PACKAGEDIR}
    cp -R ${ROOTDIR}/${MODULE}/src/main/java-deprecated/org/apache/http/* ${PACKAGEDIR}
done

cp -R ${ROOTDIR}/codec/src/main/java/* ${SRCDIR}/

cd ${PACKAGEDIR}

# Add androidextra.HttpClientAndroidLog to the package
mkdir androidextra
cp ${SCRIPTDIR}/androidextra/* androidextra

# Apply Android bugfix https://android-review.googlesource.com/#/c/15755/1/src/org/apache/http/impl/conn/DefaultClientConnectionOperator.java
find . -name "DefaultClientConnectionOperator.java" -exec sed -i "s/conn.getSocket(), target.getHostName(), target.getPort()/conn.getSocket(), target.getHostName(), schm.resolvePort(target.getPort())/g" {} +

# Delete classes dependent on org.ietf
rm impl/auth/NegotiateScheme.java
rm impl/auth/NegotiateSchemeFactory.java
rm impl/auth/GGSSchemeBase.java
rm impl/auth/KerberosScheme.java
rm impl/auth/KerberosSchemeFactory.java
rm impl/auth/SPNegoScheme.java
rm impl/auth/SPNegoSchemeFactory.java

find . -name "*.java" -exec sed -i "/impl\.auth\.KerberosSchemeFactory;/c \/\* KerberosSchemeFactory removed by HttpClient for Android script. \*\/" {} +
find . -name "*.java" -exec sed -i "/impl\.auth\.SPNegoSchemeFactory;/c \/\* SPNegoSchemeFactory removed by HttpClient for Android script. \*\/" {} +
find . -name "*.java" -exec sed -i "/impl\.auth\.NegotiateSchemeFactory;/c \/\* NegotiateSchemeFactory removed by HttpClient for Android script. \*\/" {} +
find . -name "ProxyClient.java" -exec sed -i -n '1h;1!H;${;g;s/this.authSchemeRegistry.register([^)]*SPNegoSchemeFactory());/\/\* SPNegoSchemeFactory removed by HttpClient for Android script. \*\//g;p;}' {} +
find . -name "ProxyClient.java" -exec sed -i -n '1h;1!H;${;g;s/this.authSchemeRegistry.register([^)]*KerberosSchemeFactory());/\/\* KerberosSchemeFactory removed by HttpClient for Android script. \*\//g;p;}' {} +
find . -name "AbstractHttpClient.java" -exec sed -i -n '1h;1!H;${;g;s/registry.register([^)]*SPNegoSchemeFactory());/\/\* SPNegoSchemeFactory removed by HttpClient for Android script. \*\//g;p;}' {} +
find . -name "AbstractHttpClient.java" -exec sed -i -n '1h;1!H;${;g;s/registry.register([^)]*KerberosSchemeFactory());/\/\* KerberosSchemeFactory removed by HttpClient for Android script. \*\//g;p;}' {} +
find . -name "AbstractHttpClient.java" -exec sed -i -n '1h;1!H;${;g;s/registry.register([^)]*NegotiateSchemeFactory());/\/\* NegotiateSchemeFactory removed by HttpClient for Android script. \*\//g;p;}' {} +
find . -name "HttpClientBuilder.java" -exec sed -i -n '1h;1!H;${;g;s/.register([^)]*SPNegoSchemeFactory())/\/\* SPNegoSchemeFactory removed by HttpClient for Android script. \*\//g;p;}' {} +
find . -name "HttpClientBuilder.java" -exec sed -i -n '1h;1!H;${;g;s/.register([^)]*KerberosSchemeFactory())/\/\* KerberosSchemeFactory removed by HttpClient for Android script. \*\//g;p;}' {} +

# Replace logging stuff
find . -name "*.java" -exec sed -i "/commons\.logging\.Log;/c import org\.apache\.http\.androidextra\.HttpClientAndroidLog;" {} +
find . -name "*.java" -exec sed -i "/commons\.logging\.LogFactory;/c \/\* LogFactory removed by HttpClient for Android script. \*\/" {} +
find . -name "*.java" -exec sed -i 's/ Log \(.*\)/ HttpClientAndroidLog \1/g' {} +
find . -name "*.java" -exec sed -i 's/LogFactory.getLog(\(.*\))/new HttpClientAndroidLog(\1)/g' {} +

# Rename package
find . -name "*.java" -exec sed -i "s/org\.apache\.http/${PACKAGENAME}/g" {} +

cd ${ANDROIDPROJECTPATH}
sed -i "s/ACTIVITY_ENTRY_NAME/${PROJECTNAME}/g" AndroidManifest.xml
sed -i '/<\/project>/ i <path id="android\.libraries\.src"><path refid="project\.libraries\.src" \/><\/path><path id="android\.libraries\.jars"><path refid="project\.libraries\.jars" \/><\/path>' build.xml
cd ${ROOTDIR}
tar cvfz httpclientandroidlib-${HTTPCLIENTANDROIDLIB_VER}.tar.gz ${PROJECTNAME}
cd ${ANDROIDPROJECTPATH}
ant release
cd bin
mv classes.jar ${ROOTDIR}/${PROJECTNAME}-${HTTPCLIENTANDROIDLIB_VER}.jar
cd ${ROOTDIR}
